from rest_framework import serializers
from .models import UserProfile
import re


class UserSer(serializers.ModelSerializer):
    # 自定义正则验证手机号
    def validate_phone(self, phone):
        if not re.match("^1[3-9]\d{9}$",phone):
            # 不是手机号 抛出异常
            raise serializers.ValidationError(detail='手机号格式错误',code=400)
        return phone

    class Meta:
        model=UserProfile
        fields='__all__'